package com.kgc.cgprovider.service.impl;

import com.kgc.basic.util.ResponseMsg;
import com.kgc.cgprovider.dao.CGDao;
import com.kgc.cgprovider.service.CGServ;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class CGServImpl implements CGServ {

    Logger logger = LoggerFactory.getLogger(CGServImpl.class);

    @Autowired
    private CGDao cgDao;

    @Override
    public String getCode(String table) {
        Date data = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String date = format.format(data);
        Map<String,String> map = new HashMap<String,String>();
        map.put("table",table);
        map.put("code",date);
        int i = cgDao.countNum(map);
        StringBuffer sb = new StringBuffer(String.valueOf(i));
        for(i = sb.length() ; i< 4 ; i++){
            sb.insert(0,"0");
        }
        String code =date +"-"+ sb.toString();
        logger.info("getCode map"+ map + "num"+i);
        return code;
    }


    @Override
    public ResponseMsg del(String table, String code) {
        logger.info("del code{} table{}",code,table );
        Map<String,String> map = new HashMap<>();
        map.put("table",table);
        map.put("code",code);
        int i = cgDao.del(map);
        ResponseMsg responseMsg = new ResponseMsg();
        if(i>0){
            responseMsg.setCode(200);
        }else {
            responseMsg.setCode(500);
        }
        return responseMsg;
    }

    @Override
    public boolean delInfo(String table, int id) {
        Map<String,Object> map = new HashMap<>();
        map.put("table",table);
        map.put("id",id);
        int i = cgDao.delInfo(map);
        return i>0;
    }

    @Override
    public boolean closeOrder(String table, String code) {
        Map<String , Object> map = new HashMap<>();
        map.put("table",table);
        map.put("code",code);
        int i = cgDao.closeOrder(map);
        return i > 0;
    }
}
